package com.csxy.lsh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.lsh.controller.EduTeacherController;
import com.csxy.lsh.entity.CourseInfoForm;
import com.csxy.lsh.entity.EduCourse;
import com.csxy.lsh.entity.EduCourseDescription;
import com.csxy.lsh.entity.vo.CourseFrontVo;
import com.csxy.lsh.entity.vo.CoursePublishVo;
import com.csxy.lsh.entity.vo.CourseWebVo;
import com.csxy.lsh.exception.MyException;
import com.csxy.lsh.mapper.EduCourseMapper;
import com.csxy.lsh.service.EduChapterService;
import com.csxy.lsh.service.EduCourseDescriptionService;
import com.csxy.lsh.service.EduCourseService;
import com.csxy.lsh.entity.vo.CourseQuery;
import com.csxy.lsh.service.EduVideoService;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EduCourseServiceImpl  extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;
    @Override
    public String addCourseInfo(CourseInfoForm courseInfoForm) {
          EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        int result = baseMapper.insert(eduCourse);



        String cid = eduCourse.getId();

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());

        eduCourseDescription.setId(cid);

        return cid;
    }


    @Override
    public CourseInfoForm getCourseInfo(String courseId) {
        CourseInfoForm courseInfoForm = new CourseInfoForm();

        EduCourse eduCourse = baseMapper.selectById(courseId);

        BeanUtils.copyProperties(eduCourse,courseInfoForm);



        courseInfoForm.setDescription(eduCourseDescriptionService.getById(courseId).getDescription());

        return courseInfoForm;
    }

    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {

        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        baseMapper.updateById(eduCourse);
        //修改描述信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescription.setId(courseInfoForm.getId());
        eduCourseDescriptionService.updateById(eduCourseDescription);


    }

    @Override
    public boolean removeCourse(String id) {
        eduCourseDescriptionService.removeById(id);

        //根据课程id删除小节
        eduVideoService.removeVideoByCourseId(id);

        //根据课程id删除章节部分
        eduChapterService.removeChapterByCourseId(id);
        //根据id删除个人简介
        eduCourseDescriptionService.removeById(id);

        //根据课程

        int result = baseMapper.deleteById(id);

        if (result>=1){
            return true;
        }else {
            throw new MyException(20001,"删除失败");
        }

    }

    @Override
    public void pageQuery(Page<EduCourse> pageCourse, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if (!title.isEmpty()){
            queryWrapper.like("title",title);
        }
        if (!status.isEmpty()){
            queryWrapper.eq("status",status);
        }
        queryWrapper.orderByDesc("gmt_modified");
        baseMapper.selectPage(pageCourse,queryWrapper);
    }
    @Autowired
    private EduCourseMapper eduCourseMapper;

    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        return eduCourseMapper.getPublishCourseInfo(id);
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }

    @Override
    @Cacheable(key = "'selectHotCourse'",value = "course")
    public List<EduCourse> selectHotCourse() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 8");
        return baseMapper.selectList(wrapper);
    }

    @Override
    public Map<String, Object> getCourseFrontInfo(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(courseFrontVo.getSubjectParentId())) {
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getSubjectId())) {
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getBuyCountSort())) {
            wrapper.orderByDesc("buy_count");
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getGmtCreateSort())) {
            wrapper.orderByDesc("gmt_create");

        }
        eduCourseMapper.selectPage(pageCourse, wrapper);
        Map<String, Object> map = new HashMap<>();
        long total = pageCourse.getTotal();
        List<EduCourse> courseList = pageCourse.getRecords();
        long size = pageCourse.getSize();
        long current = pageCourse.getCurrent();
        long pages = pageCourse.getPages();
        boolean hasPrevious = pageCourse.hasPrevious();
        boolean hasNext = pageCourse.hasNext();
        map.put("total", total);
        map.put("list", courseList);
        map.put("size", size);
        map.put("current", current);
        map.put("pages", pages);
        map.put("hasPrevious", hasPrevious);
        map.put("hasNext", hasNext);
    return map;
    }
}
